from elasticsearch_dsl import Document, Date, Completion, Text, Keyword, HalfFloat
from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer
from elasticsearch_dsl.connections import connections

# 新建连接
connections.create_connection(hosts="127.0.0.1")
# 自定义分析器
class CustomAnalyzer(_CustomAnalyzer):
    def get_analysis_definition(self):
        return {}
ik_analyzer = CustomAnalyzer("ik_max_word", filter=["lowercase"])

class NovelType(Document):
    # 为type创建mapping
    suggestion = Completion(analyzer=ik_analyzer)
    book_href=Keyword()
    book_name=Text("ik_max_word")
    author_name=Keyword()
    novel_status=Keyword()
    novel_authority=Keyword()
    novel_size=Keyword()
    publish_time=Date()
    author_recommend=Keyword()
    related_url=Keyword()
    download_address=Keyword()
    PageRank=HalfFloat()
    content_introduction=Text("ik_max_word") #中文分词器
    #创建索引
    class Index:
        name = 'novelindex'
        settings = {
          "number_of_shards": 5, #设主分片数为5
        }

if __name__ == "__main__":
    NovelType.init()